package com.eyedsion.his.web.service.sycndata;

import com.eyedsion.his.web.dao.dec.syncdata.DecSyncLogRepository;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * @author 彭长云
 * @Description:
 * @date 2018/12/19 17:53
 */
@Service
public class DecSyncLogService{
    @Autowired
    private EntityManager entityManager;

    public List<Map<String,Object>> getlastUpdateRecord(){
        String sql ="select * from(select dsl.type,dsl.remark, decode(dsl.sync_type, 1, '全量', 2, '增量') as syncType," +
                "to_char(dsl.dec_create_time, 'yyyy-mm-dd hh24:mi:ss') as decCreateTime," +
                "row_number() over(partition by dsl.type order by dsl.dec_create_time desc) cn " +
                "from dec_sync_log dsl where dsl.type in ('baseData','businessData')" +
                ") t where t.cn =1";
        Query query = entityManager.createNativeQuery(sql);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return query.getResultList();
    }
}
